#
# This file is a part of the open-eBackup project.
# This Source Code Form is subject to the terms of the Mozilla Public License, v. 2.0.
# If a copy of the MPL was not distributed with this file, You can obtain one at
# http://mozilla.org/MPL/2.0/.
#
# Copyright (c) [2024] Huawei Technologies Co.,Ltd.
#
# THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
# EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
# MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
#

from enum import Enum
from pydantic import BaseModel, Field

from common.const import BackupTypeEnum, ReportDBLabel, CopyDataTypeEnum


class RestoreType(int, Enum):
    FULL_RESTORE = 1  # 全量副本恢复
    TIMESTAMP_RESTORE = 2  # 时间点恢复


class GstSingleConst:
    """
    GaussDB T单机版常量类
    """
    # GaussDB T默认管理员用户
    SYS_DBA = "SYS"
    # GaussDB T环境变量白名单
    GST_ENV_VAR_WHITELIST = ("GSDB_HOME", "GSDB_DATA")
    # GaussDB T时间格式：2023-07-01 10:01:01
    GSDB_TIME_FORMAT = "%Y-%m-%d %H:%M:%S"
    # 备份超时时间（单位：s）--1天
    BACKUP_TIMEOUT = 86400
    PARAM_NODE_IP = "node_ip"
    PARAM_JOB_ID = "job_id"
    PARAM_SUB_JOB_ID = "sub_job_id"
    # 备份类型和备份目录前缀映射关系
    BACKUP_TYPE_DIR_PREFIX_MAP = {
        BackupTypeEnum.FULL_BACKUP.value: "f",
    }
    # 方法名和(事件标签,参数名列表)映射关系
    FUNC_LABEL_PARAMS_MAP = {
        "allow_backup_in_local_node": (None, list()),
        "query_job_permission": (None, list()),
        "check_backup_job_type": (None, list()),
        "exec_backup_pre_job": (None, list()),
        "exec_backup": (ReportDBLabel.BACKUP_SUB_FAILED, [PARAM_SUB_JOB_ID]),
        "exec_backup_post_job": (ReportDBLabel.POST_TASK_FAIL, [PARAM_NODE_IP, PARAM_SUB_JOB_ID]),
        "abort_job": (None, list())
    }
    # 备份副本所在单机和恢复目标单机的安装目录不一样，请确保在配置文件zengine.ini中配置了DB_FILE_NAME_CONVER和LOG_FILE_NAME_CONVERT。
    # 请参考《OceanProtect 备份一体机 故障处理》或联系技术支持工程师协助处理。
    RESTORE_DIFFERENT_INSTALL_DIR_WARN_LABEL = "job_restore_different_install_dir_warn_label"
    JSON_SCHEMA_PATH = "gaussdbt/single/jsonschema/gaussdbt_single_define.json"


class GstSingleKeyConst:
    # application中操作系统用户名
    APP_AUTH_AUTHKEY = "application_auth_authKey"
    # 备份protectObject中操作系统用户名
    JOB_PROTECTOBJ_AUTH_AUTHKEY = "job_protectObject_auth_authKey"
    # 恢复targetObject中操作系统用户名
    JOB_TARGETOBJ_AUTH_AUTHKEY = "job_targetObject_auth_authKey"
    # 数据库认证类型
    SYS_AUTH_TYPE = "sysType"
    # 数据库认证密码
    PROTECT_OBJ_SYS_AUTH_PWD = "job_protectObject_auth_extendInfo_sysAuthPwd"
    # 恢复数据库认证密码
    TARGET_OBJ_SYS_AUTH_PWD = "job_targetObject_auth_extendInfo_sysAuthPwd"
    # 备份集开始时间，样例：2023-07-01 10:01:01.578900
    BAK_SET_START_TIME = "BACKUP_SET_START_TIME"
    # 备份集完成时间，样例：2023-07-01 10:01:02.578900
    BAK_SET_COMPLETION_TIME = "BACKUP_SET_COMPLETION_TIME"
    # 日志备份开始时间
    LOG_BAK_START_TIME = "LOG_BAK_START_TIME"
    # 日志备份结束时间
    LOG_BAK_END_TIME = "LOG_BAK_END_TIME"
    # 备份数据目录名称
    BAK_DIR_NAME = "BackupDirName"
    # 日志备份时依赖的上一个副本结束时间戳
    PRE_COPY_END_TIMESTAMP = "PreCopyEndTimestamp"
    # 资源接入数据库认证密码
    APP_SYS_AUTH_PWD = "application_auth_extendInfo_sysAuthPwd"
    # 指定单机数据文件路径，即GSDB_DATA路径
    GSDB_DATA_DIR = "GsdbDataDir"


class SingleRestoreParam(BaseModel):
    # 恢复参数
    os_user: str = Field(None, description="db user")
    zbackup_file: str = Field(None, description="zbackup.py path")
    gsdb_data: str = Field(None, description="GSDB_DATA path")
    full_copy_path: str = Field(None, description="full copy path")
    gsdb_home: str = Field(None, description="gsdb install path")
    instance_role: str = Field(default="primary", description="instance role")
    archive_log_copy_path: str = Field(None, description="archive log copy path")
    restore_time: str = Field(None, description="restore time")


class ArchiveType:
    archive_array = [CopyDataTypeEnum.TAP_ARCHIVE.value, CopyDataTypeEnum.S3_ARCHIVE.value]
